package com.lin.common.utils.security;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * AES加密
 * @author lingo
 *
 */
public class AESCipherUtils {

	public static byte[] decrypt(String key, byte[] input) throws UnsupportedEncodingException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
		byte[] keyBytes = MD5Utils.getMD5Str16(key).getBytes();
		return AES.decrypt(keyBytes, keyBytes, input);
	}

	public static String encrypt(String key, byte[] input) throws UnsupportedEncodingException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
		byte[] keyBytes = MD5Utils.getMD5Str16(key).getBytes();
		return Base64.getEncoder().encodeToString(AES.encrypt(keyBytes, keyBytes, input));
	}

}
